<?php

namespace app\admin\controller;

use app\common\controller\Admin;
use app\common\model\Cate as ContentModel;

/**
 * @title 内容管理
 */
class Cate extends Admin {

    public function _initialize() {
        parent::_initialize();
    }

    /**
     * @title 分类列表
     */
    public function category_index() {
        $param = [];
        $order = 'sort asc';
        $list = ContentModel::getCategoryList($param, $order);
        $this->assign('list', $list);
        $this->setMeta('分类管理');
        return view('category/index');
    }

    /**
     * @title 分类添加
     */
    public function category_add() {
        $place = model('Cate');
        if ($this->request->isPost()) {
            $data=$this->request->param();
            if(strlen($data['name'])>30){
                return $this->error('分类名称不能超过30个字');
            }
            $result = model('category')->save($data);
            if ($result !== false) {
                return $this->success("添加成功", url('category_index'));
            } else {
                return $this->error($place->getError());
            }
        } else {
            $data = array(
                'keyList' => $place->keyList,
            );
            $this->assign($data);
            $this->setMeta("添加分类");
            return $this->fetch('public/edit');
        }
    }

    /**
     * @title 分类编辑
     */
    public function category_edit($id) {
        $place = model('Cate');
        if ($this->request->isPost()) {
            $data=$this->request->param();
            if(strlen($data['name'])>300){
                return $this->error('分类名称不能超过100个字');
            }
            $result = model('category')->save($data, ['id' => $id]);
            if ($result !== false) {
                return $this->success("编辑成功", url('category_index'));
            } else {
                return $this->error($place->getError());
            }
        } else {
            $info = db('category')->where(array('id' => $id))->find();
            if (!$info) {
                return $this->error("非法操作！");
            }
            $data = array(
                'info' => $info,
                'keyList' => $place->keyList,
            );
            $this->assign($data);
            $this->setMeta("编辑分类");
            return $this->fetch('public/edit');
        }
    }

    /**
     * @title 分类删除
     */
    public function category_delete() {
        $param = $this->request->param();
        $id = $param['id'];
        if (empty($id)) {
            return $this->error("非法操作！");
        }

        $map['id'] = array('IN', $id);
        $result = model('category')->where($map)->delete();

        if (false !== $result) {
            //记录行为
            action_log('delete_content', 'content', $result, session('auth_user.uid'));
            return $this->success("删除成功！");
        } else {
            return $this->error("删除失败！");
        }
    }

    /**
     * @title 内容添加
     * @author molong <ycgpp@126.com>
     */
    public function add() {
        if ($this->request->isPost()) {
            $result = $this->model->save($this->request->param());
            if ($result) {
                //记录行为
                action_log('add_content', 'content', $result, session('auth_user.uid'));
                return $this->success("添加成功！", url('admin/content/index', array('model_id' => $this->modelInfo['id'])));
            } else {
                return $this->error($this->model->getError(), url('admin/content/add', array('model_id' => $this->modelInfo['id'])));
            }
        } else {
            $info = array(
                'model_id' => $this->modelInfo['id'],
            );
            $data = array(
                'info' => $info,
                'fieldGroup' => $this->getField($this->modelInfo),
            );
            if ($this->modelInfo['template_add']) {
                $template = 'content/' . $this->modelInfo['template_add'];
            } else {
                $template = 'public/edit';
            }
            $this->assign($data);
            $this->setMeta("添加" . $this->modelInfo['title']);
            return $this->fetch($template);
        }
    }

    /**
     * @title 内容修改
     * @author molong <ycgpp@126.com>
     */
    public function edit($id) {
        if ($this->request->isPost()) {
            $result = $this->model->save($this->request->param(), array('id' => $id));
            if ($result !== false) {
                //记录行为
                action_log('update_content', 'content', $result, session('auth_user.uid'));
                return $this->success("更新成功！", url('admin/content/index', array('model_id' => $this->modelInfo['id'])));
            } else {
                return $this->error($this->model->getError(), url('admin/content/edit', array('model_id' => $this->modelInfo['id'], 'id' => $id)));
            }
        } else {
            if (!$id) {
                return $this->error("非法操作！");
            }
            $info = $this->model->find($id);
            if (!$info) {
                return $this->error($this->model->getError());
            }
            $info['model_id'] = $this->modelInfo['id'];
            $data = array(
                'info' => $info,
                'fieldGroup' => $this->getField($this->modelInfo),
            );
            if ($this->modelInfo['template_edit']) {
                $template = 'content/' . $this->modelInfo['template_edit'];
            } else {
                $template = 'public/edit';
            }
            $this->assign($data);
            $this->setMeta("编辑" . $this->modelInfo['title']);
            return $this->fetch($template);
        }
    }

    /**
     * @title 内容删除
     * @author molong <ycgpp@126.com>
     */
    public function del() {
        $param = $this->request->param();
        $id = $param['id'];
        if (empty($id)) {
            return $this->error("非法操作！");
        }

        $map['id'] = array('IN', $id);
        $result = $this->model->where($map)->delete();

        if (false !== $result) {
            //记录行为
            action_log('delete_content', 'content', $result, session('auth_user.uid'));
            return $this->success("删除成功！");
        } else {
            return $this->error("删除失败！");
        }
    }

    /**
     * @title 设置状态
     * @author molong <ycgpp@126.com>
     */
    public function status($id, $status) {
        $map['id'] = $id;
        $result = $this->model->where($map)->setField('status', $status);
        if (false !== $result) {
            return $this->success("操作成功！");
        } else {
            return $this->error("操作失败！！");
        }
    }

    /**
     * @title 设置置顶
     * @author molong <ycgpp@126.com>
     */
    public function settop($id, $is_top) {
        $map['id'] = $id;
        $result = $this->model->where($map)->setField('is_top', $is_top);
        if (false !== $result) {
            return $this->success("操作成功！");
        } else {
            return $this->error("操作失败！！");
        }
    }

    /**
     * @title 获取字段信息
     * @return array 字段数组
     * @author molong <ycgpp@126.com>
     */
    protected function getField() {
        $field_group = parse_config_attr($this->modelInfo['attribute_group']);

        $map['model_id'] = $this->modelInfo['id'];
        if ($this->request->action() == 'add') {
            $map['is_show'] = array('in', array('1', '2'));
        } elseif ($this->request->action() == 'edit') {
            $map['is_show'] = array('in', array('1', '3'));
        }

        //获得数组的第一条数组
        $rows = model('Attribute')->getFieldlist($map, 'id');
        if (!empty($rows)) {
            foreach ($rows as $key => $value) {
                $list[$value['group_id']][] = $value;
            }
            foreach ($field_group as $key => $value) {
                $fields[$value] = isset($list[$key]) ? $list[$key] : array();
            }
        } else {
            $fields = array();
        }
        return $fields;
    }

    /**
     * @title 创建搜索
     * @return [array] [查询条件]
     */
    protected function buildMap() {
        $map = array();
        $data = $this->request->param();
        foreach ($data as $key => $value) {
            if ($value) {
                if ($key == 'keyword') {
                    $map['title'] = array("LIKE", "%$value%");
                } elseif ($key == 'category') {
                    $map['category_id'] = $value;
                } elseif ($key == 'create_time') {
                    $map['create_time'] = array('BETWEEN', array(strtotime($value[0]), strtotime($value[1])));
                } else {
                    $map[$key] = $value;
                }
            }
        }
        if (isset($map['page'])) {
            unset($map['page']);
        }
        if (isset($map['model_id'])) {
            unset($map['model_id']);
        }
        $this->assign($data);
        return $map;
    }

}
